<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\v1;

use App\Exception\BusinessException;
use App\Service\UserService;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ResponseInterface;


#[Controller(prefix: "/api/v1/auth")]
class AuthController extends AbstractController
{
    #[Inject]
    private UserService $userService;

    /**
     * 用户注册接口
     * @param RequestInterface $request
     * @return array|ResponseInterface
     */
    #[PostMapping(path: 'register')]
    public function register(RequestInterface $request): array|ResponseInterface
    {
        // 获取注册数据
        $user['name'] = $request->input('name', '');
        $user['email'] = $request->input('email', '');
        $user['password'] = $request->input("password", '');
        $user['password_confirmation'] = $request->input("password_confirmation", '');

        // 调用用户服务->注册用户
        try {
            $ret = $this->userService->createUser($user);
        } catch (BusinessException $e) {
            return $this->fail(40002,"注册失败",[ "errors" => $e->getMessage()]);
        }

        return $this->success($ret, "注册成功");
    }

    /**
     * 用户登录接口
     * @param RequestInterface $request
     * @return array|ResponseInterface
     */
    #[PostMapping(path: 'login')]
    public function login(RequestInterface $request): array|ResponseInterface
    {
        $email = $request->input('email', '');
        $password = $request->input("password", '');
        $server_info = $request->getServerParams();
        $ip = $server_info['remote_addr'];
        try {
            $ret = $this->userService->login($email, $password, $ip);
        } catch (BusinessException $e) {
            return $this->fail(40002, "登录失败", ["errors" => $e->getMessage()]);
        }
        return $this->success($ret, "登录成功");
    }

    /**
     * 用户个人信息获取接口
     * @param RequestInterface $request
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    #[GetMapping("me")]
    public function me(RequestInterface $request): array|ResponseInterface
    {
        $userId = Context::get("login_user_id") . "";
        try {
            // 获取用户信息
            $userData = $this->userService->getUserById($userId, true);

            // 删除敏感信息
            unset($userData["password"]);

            // 获取用户stats 数据
            $userData['stat'] = [];

            // 获取用户签到信息
            $userData['today_is_sign'] = [];

        } catch (BusinessException $e) {
            return $this->fail(40002, "请求失败", ["errors" => $e->getMessage()]);
        }

        return $this->success($userData);

    }


}
